<?php

class Admin_cars_model extends CI_Model
{

	//A class with the functions for creating cars and modidy cars
	
	
	function RegisterCar($regPlate,$seats, $model, $disc, $sizeClass, $smallImage, $bigImage, $keyNumber)
	{	
	
		$this->load->helper('security');
		
		 
		//	$sqlCommand='INSERT INTO kund VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
	
		
		$sqlCommand='INSERT INTO bil (`RegPlåt`, `Sittplatser`, `Modell`, `Info`, `Storleksklass`, `LitenBild`, `StorBild`, `NyckelNR`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
		
		$result=$this->db->query($sqlCommand,array($regPlate,$seats, $model, $disc, $sizeClass, $smallImage, $bigImage, $keyNumber));
		
		if ($result ==1)
		{	
			return $result;
		}
		else
		{
			return FALSE;
		}
	
	}





	function saveNewRegPlate($id, $newRegPlate)
	{
		
		
		
			$sqlCommand='UPDATE bil SET RegPlåt= ? WHERE ID = ?';
			$query=$this->db->query($sqlCommand,array($newRegPlate, $id));
		
			return $query;
		
	}
	
	
	function saveNewSeats($id, $newSeats)
	{
		
		
		
			$sqlCommand='UPDATE bil SET Sittplatser= ? WHERE ID = ?';
			$query=$this->db->query($sqlCommand,array($newSeats, $id));
		
			return $query;
		
	}
	
	function saveNewModel($id, $newModel)
	{
		
		
		
			$sqlCommand='UPDATE bil SET Modell= ? WHERE ID = ?';
			$query=$this->db->query($sqlCommand,array($newModel, $id));
		
			return $query;
		
	}
	
	
	
	function saveNewDisc($id, $newDisc)
	{
		
		
		
			$sqlCommand='UPDATE bil SET Info= ? WHERE ID = ?';
			$query=$this->db->query($sqlCommand,array($newDisc, $id));
		
			return $query;
		
	}


	function saveNewSizeClass($id, $newSizeClass)
	{
		
		
		
			$sqlCommand='UPDATE bil SET Storleksklass= ? WHERE ID = ?';
			$query=$this->db->query($sqlCommand,array($newSizeClass, $id));
		
			return $query;
		
	}
	
	
	function saveNewSmallImage($id, $newSmallImage)
	{
		
		
		
			$sqlCommand='UPDATE bil SET LitenBild= ? WHERE ID = ?';
			$query=$this->db->query($sqlCommand,array($newSmallImage, $id));
		
			return $query;
		
	}
	
	
	function saveNewBigImage($id, $newBigImage)
	{
		
		
		
			$sqlCommand='UPDATE bil SET StorBild= ? WHERE ID = ?';
			$query=$this->db->query($sqlCommand,array($newBigImage, $id));
		
			return $query;
		
	}
	
	
	function saveNewKeyNumber($id, $newKeyNumber)
	{
		
		
		
			$sqlCommand='UPDATE bil SET NyckelNR= ? WHERE ID = ?';
			$query=$this->db->query($sqlCommand,array($newKeyNumber, $id));
		
			return $query;
		
	}
	
	function getCarPrices($carID, $priceClassID)
	{
		
				
		//Function that gets data about the prices for a specific car
		$sqlCommand='SELECT * FROM bilpris JOIN bil on bilpris.BilID = bil.ID WHERE bilpris.BilID = ? AND bilpris.PrisklassID = ?';
		$result=$this->db->query($sqlCommand, array($carID, $priceClassID));
		
		if ($result->num_rows()==1)
		{	
			return $result->row_object(0);
			
		}
		else
		{
			return FALSE;
		}	
	}
		
	function saveCarWeekPrice($carID, $priceClassID, $newPrice)
	{
			$sqlCommand='UPDATE bilpris SET VeckoPris= ? WHERE BilID = ? AND PrisklassID = ?';
			$query=$this->db->query($sqlCommand,array($newPrice, $carID, $priceClassID));
		
			return $query;
	}
	
	function saveCarDayPrice($carID, $priceClassID, $newPrice)
	{
			$sqlCommand='UPDATE bilpris SET DagPris= ? WHERE BilID = ? AND PrisklassID = ?';
			$query=$this->db->query($sqlCommand,array($newPrice, $carID, $priceClassID));
		
			return $query;
	}
	
	function saveCarHourPrice($carID, $priceClassID, $newPrice)
	{
			$sqlCommand='UPDATE bilpris SET TimmarPris= ? WHERE BilID = ? AND PrisklassID = ?';
			$query=$this->db->query($sqlCommand,array($newPrice, $carID, $priceClassID));
		
			return $query;
	}
	
	function insertCarPrices($carID, $priceClassID, $weekPrice, $dayPrice, $hourPrice)
	{
		//creating a new row in bilpris with a cars prices		
		$sqlCommand='INSERT INTO bilpris (`BilID`, `PrisklassID`, `VeckoPris`, `DagPris`, `TimmarPris`) VALUES (?, ?, ?, ?, ?)';
		
		$result=$this->db->query($sqlCommand,array($carID, $priceClassID, $weekPrice, $dayPrice, $hourPrice));
		
		if ($result ==1)
		{	
			return $result;
		}
		else
		{
			return FALSE;
		}
	}
	
	function insertPriceClass($name, $info)
	{
		//creating a new row prisklass with info for a new priceclass		
		$sqlCommand='INSERT INTO prisklass (`Namn`, `Forklaring`) VALUES (?, ?)';
		
		$result=$this->db->query($sqlCommand,array($name, $info));
		
		if ($result ==1)
		{	
			return $result;
		}
		else
		{
			return FALSE;
		}
	}
	
	function getPriceClassID($name)
	{
			
		
		$sqlCommand='SELECT * FROM prisklass WHERE Namn= ?';
		$result=$this->db->query($sqlCommand, array($name));
		
		if ($result->num_rows()==1)
		{	
			return $result->row_object(0);
			
		}
		else
		{
			return FALSE;
		}	
	}
}